package chapter6;

import java.util.Scanner;

/****************************************************************
 * Author：icynlau
 * Description: 计算一个字符串中指定字符出现的次数
运行结果

Enter the string:  wdadwa
Enter a character:  a
The character has occured 2 times

 * Date:2021/4/4
 * ***************************************************************
 */
public class exc6_23 {
	
	public static void main(String[] args) {
		
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the string:  ");
		String s=input.nextLine();
		System.out.print("Enter a character:  ");
		char ch=input.next().charAt(0);
		
		System.out.printf("The character has occured %d times\n",count(s,ch));
	}
	
	public static int count(String str,char a) {
		
		int sum=0;
		
		for(int i=0;i<str.length();i++) {
			
			if(str.charAt(i)==a) {
				
				sum++;
				
			}
			
		}
		
		return sum;
	}

}
